<script>
/**
 * vuex管理登录状态，具体可以参考官方登录模板示例
 */
import { mapMutations } from "vuex";
import APPUpdate from "@/plugins/APPUpdate";
import { getClipboardData } from "@/js_sdk/h5-copy/h5-copy.js";
import config from "@/config/config";
import storage from "@/utils/storage";

/**
 * 路由监听并删除路由
 * https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
 * */
// #ifdef MP-WEIXIN
wx.onAppRoute((res) => {
	const pages = getCurrentPages();
	console.log("pages:" + pages.length, pages);
	if (pages.length > 3) {
		delete getCurrentPages()[2];
	}
	console.log("路由监听", {
		res
	});
});
// #endif

export default {
	data() {
		return {
			config
		};
	},
	/**
	 * 监听返回
	 */
	onBackPress(e) {
		if (e.from == "backbutton") {
			let routes = getCurrentPages();
			let curRoute = routes[routes.length - 1].options;
			routes.forEach((item) => {
				if (item.route == "pages/tabbar/cart/cartList" || item.route.indexOf("pages/product/goods") != -1) {
					uni.redirectTo({
						url: item.route
					});
				}
			});

			if (curRoute.addId) {
				uni.reLaunch({
					url: "/pages/tabbar/cart/cartList"
				});
			} else {
				uni.navigateBack();
			}
			return true; //阻止默认返回行为
		}
	},
	methods: {
		...mapMutations(["login"])
	},
	onLaunch: function () {
		this.cancelnav()
		// #ifdef APP-PLUS
		this.checkArguments(); // 检测启动参数
		APPUpdate();

		// 重点是以下： 一定要监听后台恢复 ！一定要
		plus.globalEvent.addEventListener("newintent", (e) => {
			this.checkArguments(); // 检测启动参数
		});
		// #endif

		// #ifdef MP-WEIXIN
		this.applyUpdateWeChat();
		// #endif

		// #ifdef APP-PLUS
		/**
		 * 监听通知消息事件
		 * {
		 *	 __UUID__: string  标识
		 * 	 title: string	标题
		 * 	 appid: string  打包的id
		 *   content: string 内容
		 *   payload: JSON 推送消息承载的数据,如果推送消息中传输的数据不符合JSON格式，则作为String类型数据保存。
		 * }
		 */
		plus.push.addEventListener("click", (msg) => {
			// uni.showModal({
			// 	title: "收到click消息",
			// 	content: JSON.stringify(msg)
			// });
			// 处理点击消息的业务
			this.pullPage(msg);
		});
		//监听透传消息事件
		plus.push.addEventListener("receive", (msg) => {
			// uni.showModal({
			// 	title: "收到receive的消息",
			// 	content: JSON.stringify(msg)
			// });
			// 处理服务器消息的业务逻辑代码
			this.pushMessage(msg);
		});
		// #endif
	},

	onShow() {
		// #ifndef H5
		if (this.config.enableGetClipboard) {
			this.getClipboard();
		}
		// #endif

		// #ifdef APP-PLUS
		if (storage.getShow()) {
			if (uni.getSystemInfoSync().platform == "ios") {
				this.$u.route("/pages/tabbar/screen/fullScreen");
			}
		}
		// #endif
	},
	methods: {
		check(){
			return (
			  navigator.userAgent.toLowerCase().match(/MicroMessenger/i) ==
			  "micromessenger"
			);
		},
		cancelnav(){
			if (this.check()) {
			    let pageNav = document.getElementsByTagName("uni-page-head");
			    if (pageNav && pageNav[0]) {
			      pageNav[0].style.display = "none";
			    }
			  }
		},
		/**
		 * 微信小程序版本提交更新版本 解决缓存问题
		 */
		applyUpdateWeChat() {
			const updateManager = uni.getUpdateManager();

			updateManager.onCheckForUpdate(function (res) {
				// 请求完新版本信息的回调
			});

			updateManager.onUpdateReady(function (res) {
				uni.showModal({
					title: "更新提示",
					content: "发现新版本，是否重启应用？",
					success(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate();
						}
					}
				});
			});
			updateManager.onUpdateFailed(function (res) {
				// 新的版本下载失败
			});
		},
		//  TODO 开屏广告 后续优化添加
		launch() {
			try {
				// 获取本地存储中launchFlag标识 开屏广告
				const value = uni.getStorageSync("launchFlag");
				if (!value) {
					// this.$u.route("/pages/index/agreement");
				} else {
					//app启动时打开启动广告页
					var w = plus.webview.open(
						"/hybrid/html/advertise/advertise.html",
						"本地地址",
						{
							top: 0,
							bottom: 0,
							zindex: 999
						},
						"fade-in",
						500
					);
					//设置定时器，4s后关闭启动广告页
					setTimeout(function () {
						plus.webview.close(w);
						APPUpdate();
					}, 3000);
				}
			} catch (e) {
				// error
				uni.setStorage({
					key: "launchFlag",
					data: true,
					success: function () {
						console.log("error时存储launchFlag");
					}
				});
			}
		},

		/**
		 * 获取粘贴板数据
		 */
		async getClipboard() {
			let res = await getClipboardData();

			/**
			 * 解析粘贴板数据
			 */
			if (res.indexOf(config.shareLink) != -1 && res != this.$store.state.shareLink) {
				this.$store.state.shareLink = res;
				uni.showModal({
					title: "提示",
					content: "检测到一个分享链接是否跳转？",
					confirmText: "跳转",
					success: function (callback) {
						if (callback.confirm) {
							const path = res.split(config.shareLink)[1];
							if (path.indexOf("tabbar") != -1) {
								uni.switchTab({
									url: path
								});
							} else {
								uni.navigateTo({
									url: path
								});
							}
						}
					}
				});
			}
		},

		/**
		 * h5中打开app获取跳转app的链接并跳转
		 */
		checkArguments() {
			// #ifdef APP-PLUS
			setTimeout(() => {
				const args = plus.runtime.arguments;
				if (args) {
					const argsStr = decodeURIComponent(args);
					const path = argsStr.split("//")[1];
					if (path.indexOf("tabbar") != -1) {
						uni.switchTab({
							url: `/${path}`
						});
					} else {
						uni.navigateTo({
							url: `/${path}`
						});
					}
				}
			});
			// #endif
		},
		/**
		 * 发送通知内容
		 * @param data 消息内容
		 * https://www.html5plus.org/doc/zh_cn/push.html#plus.push.createMessage
		 */
		pushMessage(data) {
			// #ifdef APP-PLUS
			const payload = data.payload ? data.payload : {};
			// 避免ios消息透传重复通知
			if (!payload.clientNotice) {
				payload.clientNotice = true;
				plus.push.createMessage(data.content, payload, {
					appid: data.appid,
					cover: false,
					delay: 0,
					title: data.title
				});
			}
			// #endif
		},
		/**
		 * 拉起页面
		 * @param data 消息内容
		 */
		pullPage(data) {
			// 判断用户是否登录
			if (this.$options.filters.isLogin("auth")) {
				const userInfo = this.$options.filters.isLogin();
				if (userInfo.storeId) {
					// 员工
					uni.redirectTo({
						url: "/pages/staff/order/index"
					});
				} else {
					// 会员
					uni.switchTab({
						url: "/pages/tabbar/user/my"
					});
				}
			}
		}
	}
};
</script>

<style lang="scss">
@import "uview-ui/index.scss";

// -------适配底部安全区  苹果x系列刘海屏

// #ifdef MP-WEIXIN
.mp-iphonex-bottom {
	padding-bottom: constant(safe-area-inset-bottom);
	padding-bottom: env(safe-area-inset-bottom);
	box-sizing: content-box;
	height: auto !important;
	padding-top: 10rpx;
}

// #endif

body {
	background-color:#F4F6F8;
	color:#333;
	font-family: '微软雅黑';
}

/************************ */
.w200 {
	width: 200rpx !important;
}

.flex1 {
	flex: 1; //必须父级设置flex
}
.flex-row-start {
	display: flex;
	flex-direction: row;
	align-items: center;
	flex-wrap: wrap;
	justify-content: flex-start;
}
.flex-row-end{
	justify-content: flex-end;
}
.flex-row-around {
	display: flex;
	flex-direction: row;
	justify-content: space-around;
	align-items: center;
	flex-wrap: wrap;
}
.flex-row-center {
	display: flex;
	flex-direction: row;
	justify-content: center;
	align-items: center;
}
.flex-row-between {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
	flex-wrap: wrap;
}
.flex-top{
	align-items: self-start;
}
.uni-input-input:-internal-autofill-selected {
	background-color: #fff!important;
}
.width{
	width: 95%;
	margin: auto;
}
.font16{
	font-size: 16px;
}
.font18{
	font-size: 18px;
}
.font15{
	font-size: 15px;
}
.font13{
	font-size: 13px;
}
.font12{
	font-size: 12px;
}
.color6{
	color:#666;
}
.color3{
	color:#333;
}
.color1{
	color: #1C1C1C;
}
.color9{
	color:#999;
}
.yellow{
	color: #FF9100;
}
.color2{
	color:#2F3556;
}
.font20{
	font-size: 20px;
}
.bold{
	font-weight: 700;
}
.font500{
	font-weight: 500;
}
.paddingtop10{
	padding-top: 10px;
}
.bg-gray{
	background-color: #F4F6F8;
}
.bg-white{
	background-color: white;
}
.titlesp{
	font-weight: 700;
	font-size: 16px;
	color: #333;
	letter-spacing: 0;
	position: relative;
	z-index: 2;
}
.titlesp::after{
	content: "";
	position: absolute;
	z-index: -1;
	left: 0;
	background-image: -webkit-gradient(linear, right top, left top, from(#87D688), to(#87D688));
	bottom: 1px;
	width: 49px;
	height: 5px;
}
.contentpopup{
	background-color: #EEEEEE;
	padding: 12px;
	.top{
		.icon{
			width: 18px;
			height: 19px;
			margin-right: 10px;
		}
	}
	.box{
		margin: 15px auto;
	}
}
.foodappoint {
	.u-mask{
		bottom: 52px!important;
	}
	.u-drawer-bottom{
		bottom:50px!important;
	}
}
.boxin{
	padding: 0px 12px;
	border-radius: 12px;	
	.uni-input{
		text-align: right;
		font-size: 14px;
	}
	.boxitem{
		padding: 15px 0px;
		border-bottom: 1px solid #F6F6F6;
		.liuyan{
			border: 1px solid #F6F6F6;
			margin-top: 10px;
			padding: 10px;
		}
	}
	.boxitem:last-child{
		border:none;
	}
}
.commonbg{
		padding:15px 12px;
		border-radius: 12px;
		.uni-input{
			text-align: right;
			font-size: 14px;
		}
		margin-top: 14px;
		.line{
			width: 4px;
			height: 16px;
			background-color: #499C43;
			margin-right: 5px;
		}
		.rightfont{
			padding-left: 10px;
		}
		.iteminfo{
			margin-top: 15px;
			.type{
				width: 120px;
			}
		}
		.fontbottom{
			line-height: 24px;
			margin-top: 8px;
		}
	}
	.yuyue{
		margin-top: 12px;
		.time{
			width: 49%;
			overflow: hidden;
			background-color: #F4F6F8;
			padding: 8px 10px;
			border-radius: 8px;
			border:1px solid #dddddd60;
			position: relative;
			margin-bottom: 8px;
		}
		.shape{
			width: 24px;
			height: 14px;
			border-radius: 12px  0  8px  0px;
			background-color: #499C43;
			position: absolute;
			line-height: 14px;
			text-align: center;
			color: #fff;
			font-size: 12px;
			right: 0;
			bottom: 0;
			display: none;
		}
		.timecur{
			border-color: #499C43;
			background-color: #E5F6E6;
			.shape{
				display: block;
			}
		}
	}
	.greencolor{
		color: #499C43;
	}
	.listfourradio .u-radio__label{
		width: 100%!important;
		margin-right: 0!important;
	}
	.text-center{
		text-align: center;
	}
	.text-right{
		text-align: right;
	}
	.chongzhimoneyinput .u-input__input{
		font-size:35px!important;
	}
	.lineheight25{
		line-height: 25px;
	}
	.swipecarouselback{
		position: relative;
		.arricon{
			position: absolute;
			left: 2.5%;
			width: 24px;
			height: 24px;
			z-index:7;
			top: 30px;
		}
	}
.bg-gray{
	padding-bottom: 20px;
}
</style>
